package com.gangquan360.smartadmin.module.supplier.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gangquan360.smartadmin.common.domain.PageResultDTO;
import com.gangquan360.smartadmin.common.domain.ResponseDTO;
import com.gangquan360.smartadmin.module.supplier.dao.SupplierCategoryDao;
import com.gangquan360.smartadmin.module.supplier.domain.dto.SupplierCategoryDTO;
import com.gangquan360.smartadmin.module.supplier.domain.dto.SupplierCategoryQueryDTO;
import com.gangquan360.smartadmin.module.supplier.domain.entity.SupplierCategoryEntity;
import com.gangquan360.smartadmin.util.SmartPaginationUtil;
import com.gangquan360.smartadmin.util.SmartBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * [ 供应商分类表 ]
 *
 * @author wiggin
 * @version 1.0
 * @company wiggin
 * @copyright (c) 2019 wigginInc. All rights reserved.
 * @date 2020-03-14 13:55:51
 * @since JDK1.8
 */
@Service
public class SupplierCategoryService {

    @Autowired
    private SupplierCategoryDao supplierCategoryDao;


    /**
     * @author wiggin
     * @description 分页查询
     * @date 2020-03-14 13:55:51
     */
    public ResponseDTO<PageResultDTO<SupplierCategoryDTO>> queryByPage(SupplierCategoryQueryDTO queryDTO) {
        Page<SupplierCategoryDTO> page = SmartPaginationUtil.convert2PageQueryInfo(queryDTO);
        List<SupplierCategoryEntity> entities = supplierCategoryDao.queryByPage(page, queryDTO);
        List<SupplierCategoryDTO> dtoList = SmartBeanUtil.copyList(entities, SupplierCategoryDTO.class);
        page.setRecords(dtoList);
        PageResultDTO<SupplierCategoryDTO> pageResultDTO = SmartPaginationUtil.convert2PageResultDTO(page);
        return ResponseDTO.succData(pageResultDTO);
    }
    
    
    public ResponseDTO<List<SupplierCategoryDTO>> queryList() {
    	Wrapper<SupplierCategoryEntity> wrapper = new EntityWrapper<>();
		List<SupplierCategoryEntity> selectList = supplierCategoryDao.selectList(wrapper);
		List<SupplierCategoryDTO> copyList = SmartBeanUtil.copyList(selectList, SupplierCategoryDTO.class);
    	return ResponseDTO.succData(copyList);
    }
    

    /**
     * @author wiggin
     * @description 添加
     * @date 2020-03-14 13:55:51
     */
    public ResponseDTO<String> add(SupplierCategoryDTO addDTO) {
        SupplierCategoryEntity entity = SmartBeanUtil.copy(addDTO, SupplierCategoryEntity.class);
        supplierCategoryDao.insert(entity);
        return ResponseDTO.succ();
    }


    /**
     * @author wiggin
     * @description 编辑
     * @date 2020-03-14 13:55:51
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> update(SupplierCategoryDTO updateDTO) {
        SupplierCategoryEntity entity = SmartBeanUtil.copy(updateDTO, SupplierCategoryEntity.class);
        supplierCategoryDao.updateById(entity);
        return ResponseDTO.succ();
    }


    /**
     * @author wiggin
     * @description 删除
     * @date 2020-03-14 13:55:51
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> delete(Long id) {
        supplierCategoryDao.deleteById(id);
        return ResponseDTO.succ();
    }

    /**
     * @author wiggin
     * @description  根据ID查询
     * @date 2020-03-14 13:55:51
    */
    public ResponseDTO<SupplierCategoryDTO> detail(Long id){
        SupplierCategoryEntity entity = supplierCategoryDao.selectById(id);
        SupplierCategoryDTO dto =  SmartBeanUtil.copy(entity,SupplierCategoryDTO.class);
        return ResponseDTO.succData(dto);
    }
}
